package com.cloudstreamservice8002.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudstreamservice8002.entity.RouteProcess;
import com.cloudstreamservice8002.mapper.RouteProcessMapper;
import com.cloudstreamservice8002.service.RouteProcessService;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;

/**
 * @author 26407
 * @description 针对表【route_process】的数据库操作Service实现
 * @createDate 2025-06-11 00:42:49
 */
@Service
public class RouteProcessServiceImpl extends ServiceImpl<RouteProcessMapper, RouteProcess>
        implements RouteProcessService {
    @Override
    public IPage<RouteProcess> getRouteProcess(Integer pageNum, Integer pageSize, Long deliveryId, Long routeId) {
        LambdaQueryWrapper<RouteProcess> queryWrapper = new LambdaQueryWrapper<>();
        IPage<RouteProcess> page = new Page<>(pageNum, pageSize);
        if (routeId != null) {
            queryWrapper.eq(RouteProcess::getRouteId, routeId);
            return this.page(page, queryWrapper);
        }

        if (deliveryId != null) {
            queryWrapper.eq(RouteProcess::getDeliveryId, deliveryId);
            return this.page(page, queryWrapper);
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public Boolean create(RouteProcess routeProcess) {
        Preconditions.checkArgument(routeProcess.getRouteId() != null, "配送单ID不能为空");
        Preconditions.checkArgument(routeProcess.getDeliveryId() != null, "配送单ID不能为空");
        return this.save(routeProcess);
    }

    @Override
    public Boolean updates(RouteProcess routeProcess) {
        Preconditions.checkArgument(routeProcess.getId() != null, "id不能为空");
        return this.updateById(routeProcess);
    }

    @Override
    public boolean removeByCode(RouteProcess routeProcess) {
        if (routeProcess.getId() != null) {
            return this.removeById(routeProcess.getId());
        }
        if (routeProcess.getDeliveryId() != null) {
            lambdaUpdate().eq(RouteProcess::getDeliveryId, routeProcess.getDeliveryId()).remove();
        }
        if (routeProcess.getRouteId() != null) {
            lambdaUpdate().eq(RouteProcess::getRouteId, routeProcess.getRouteId()).remove();
        }
        return false;
    }
}




